<template>
  <div class='sub-category' v-if="subCate">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选分区 -->
      <SubFilter />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort></SubSort>
        <XtxInfiniteLoading
          :finished='finished'
          :loading='loading'
          @load="getlist"
          :finishedText="finishedText"
          >
           <ul class="list">
             <li v-for="item in list" :key="item.id">
                <GoodsItem :goods='item'/>
             </li>
           </ul>
        </XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-bread.vue'
import { findCategoryGoods, findSubCategory } from '@/api/category'
import { useRoute } from 'vue-router'
import { provide, reactive, ref, watch } from 'vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from '../home/components/goods-item.vue'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    const finishedText = '没有更多了喔'
    const route = useRoute()
    // console.log(route.params)
    const subCate = ref(null)
    const page = ref(1)

    // 定义一个普通对象，记录默认参数，用于重置请求参数的
    const defaultParamsFn = () => {
      return {
        // 默认排序 null
        categoryId: route.params.id, // categoryId 必传
        sortField: null, // 取值范围 [publishTime,orderNum,evaluateNum,price]
        sortMethod: null, // sortMethod <价格>排序规则：asc为正序，desc为倒序，默认为desc
        inventory: false, // inventory   是否只显示有库存
        onlyDiscount: false, // onlyDiscount  是否只显示特惠
        // attrs 属性条件数组 [{ groupName: xx, propertyName: xx },{ groupName: xx, propertyName: xx }]
        attrs: [],
        brandId: null, // brandId 品牌ID
        // page: 1, // page 当前页
        pageSize: 20 // pageSize 页容量
      }
    }
    // 根据路由得分类id 获取二级分类数据
    watch(
      () => route.params.id,
      async id => {
        // 如果当前不是二级分类页，直接退出，不发送二级请求
        if (route.path !== `/category/sub/${id}`) return
        const { result } = await findSubCategory(id)
        // console.log(result)
        subCate.value = result

        // 重置接口查询参数，重新获取默认参数对象
        const obj = defaultParamsFn()
        for (const key in obj) {
          sortParams[key] = obj[key]
        }
      }, { immediate: true })

    // 通过 provide 提供数据给所有后代组件
    provide('subCate', subCate)
    //  用于商品列表--分页查询接口查询参数
    const sortParams = reactive(defaultParamsFn())
    provide('sortParams', sortParams)
    const list = ref([])
    const loading = ref(false)
    const finished = ref(false)
    const getlist = async () => {
      // 发送请求前，显示加载中
      loading.value = true
      // 📦商品列表--分页查询      - 数据量少 - 能筛选和排序
      const { result } = await findCategoryGoods({ ...sortParams, page: page.value })
      // 📦商品列表--分页查询-临时 - 数据量大(但是数据是假的，主要用于实现分页)
      // const { result } = await findCategoryGoodsTemp({ ...sortParams, page: page.value })
      // 数据加载完成后，就隐藏加载动画
      loading.value = false
      // console.log(result)
      // list.value = result.items
      // 加载分页数据，是数组的追加操作
      list.value.push(...result.items)
      // 页码加
      page.value++
      // 如果请求回来的数据已经小于页容量，就显示加载完毕
      if (result.items.length < sortParams.pageSize) {
        finished.value = true
      }
      // console.log(sortParams.page)
    }
    watch(sortParams, () => {
      page.value = 1
      list.value = []
      getlist()
      finished.value = false
      // console.log(sortParams.page)
    }, { immediate: true })

    return { subCate, list, getlist, loading, finished, finishedText }
  }

}
</script>

<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
